package test0309;

import java.util.Scanner;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 编写程序，countLeaf(TreeNode root) 统计叶子节点个数
 */
@SuppressWarnings({"all"})
public class Main02 {

    public static void main(String[] args) {
        System.out.println("请输入扩展先序序列构建二叉树: ");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        BinaryTree.TreeNode biTree = BinaryTree.createBiTree(str);
        int countLeaf = countLeaf(biTree);
        // AB#D##C## 为例 正确的输出应该为 2 个叶子节点
        System.out.println("当前二叉树的叶子节点数量为: " + countLeaf);
    }

    // 统计二叉树有多少个叶子节点
    public static int countLeaf(BinaryTree.TreeNode root){
        if (root == null){
            return 0;
        }
        if (root.left == null && root.right == null){
            return 1;
        }
        int leftLeaf = countLeaf(root.left);
        int rightLeaf = countLeaf(root.right);
        return leftLeaf + rightLeaf;
    }
}
